import tip from '@/common/util/tip.js';
import JSEncrypt  from '@/common/util/jsencrypt.min.js';

export function GetEncryption(str){
	let pubKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2GVvca3xJ1n2yyrP2kw3V6oDLuI1EQxzB6Q4lHJo3eD5qmjRob/ldpn3lr1HBdViBqOkaqJO4beJIA6YAuwYMwJqYxQp38OrTLlfzd3TgnJ12o07mD6qysrFd2cEAB8Vwr6Mif1yK4aqUQuP7wPsxm/QNm/fFquZjb9G95PeUOwIDAQAB`;// ES6 模板字符串 引用 rsa 公钥
	 let encryptStr = new JSEncrypt();
	 encryptStr.setPublicKey(pubKey); // 设置 加密公钥
	 let data = encryptStr.encrypt(str);  // 进行加密
	 return data
}

export function GetPercent(num, total) {
	num = parseFloat(num);
	total = parseFloat(total);
	if (isNaN(num) || isNaN(total)) {
		return "-";
	}
	return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}

/*根据出生日期算出年龄*/
export function jsGetAge(strBirthday) {
	var returnAge=0;
	if(strBirthday){
		var strBirthdayArr = strBirthday.split("-");
		var birthYear = strBirthdayArr[0];
		var birthMonth = strBirthdayArr[1];
		var birthDay = strBirthdayArr[2];
		
		var d = new Date();
		var nowYear = d.getFullYear();
		var nowMonth = d.getMonth() + 1;
		var nowDay = d.getDate();
		
		if (nowYear == birthYear) {
			returnAge = 0; //同年 则为0岁
		} else {
			var ageDiff = nowYear - birthYear; //年之差
			if (ageDiff > 0) {
				if (nowMonth == birthMonth) {
					var dayDiff = nowDay - birthDay; //日之差
					if (dayDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				} else {
					var monthDiff = nowMonth - birthMonth; //月之差
					if (monthDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				}
			} else {
				returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
			}
		}
	}
	
	return returnAge; //返回周岁年龄
}
//根据身份证号输出年龄
export function GetAge(identityCard) {
	var len = identityCard.length;
	if (len == 0) {
		return 0;
	} else {
		if ((len != 15) && (len != 18)) //身份证号码只能为15位或18位其它不合法
		{
			return 0;
		}
	}
	var strBirthday = "";
	if (len == 18) //处理18位的身份证号码从号码中得到生日和性别代码
	{
		strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
		//console.log("strBirthday",parseInt(identityCard.substr(16, 1)) % 2 == 1)
	}
	if (len == 15) {
		strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
	}
	//时间字符串里，必须是“/”
	var birthDate = new Date(strBirthday);
	var nowDateTime = new Date();
	var age = nowDateTime.getFullYear() - birthDate.getFullYear();
	//再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
	if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() <
			birthDate.getDate())) {
		age--;
	}
	return age;
}
//根据身份证号输出性别
export function GetSex(identityCard) {
	var strSex = ''
	if(identityCard){
		if (identityCard.length == 18) //处理18位的身份证号码从号码中得到生日和性别代码
		{
			if (parseInt(identityCard.substr(16, 1)) % 2 == 1) {
				strSex = '男'
			} else {
				strSex = '女'
			}
		}
		if (identityCard.length == 15) {
			if (parseInt(identityCard.substr(14, 1)) % 2 == 1) {
				strSex = '男'
			} else {
				strSex = '女'
			}
		}
	}
	return strSex;
}
export function checkEndTime(startTime, endTime) {
	//console.log(startTime,endTime)
	if (startTime && endTime) {
		var sd = new Date(startTime).getTime();
		var ed = new Date(endTime).getTime();
		if (sd > ed) {
			tip.toast('结束时间要大于开始日期');
			return false;
		}
	}
	return true;
}
